package com.demo.androidgame.samgukji_hit;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;
import android.util.Log;

import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Animation;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLGraphics;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class HeroStageRenderer {

	static final float FRUSTUM_WIDTH = 800;
	static final float FRUSTUM_HEIGHT = 480;
	GLGraphics glGraphics;
	Camera2D cam;
	HeroStage herostage;
	SpriteBatcher batcher;
	float deltaTime;
	float accelX;
	S_Arrow arrow;
	
	public HeroStageRenderer (GLGraphics glGraphics, SpriteBatcher batcher, HeroStage herostage) {
		this.glGraphics = glGraphics;
        cam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
        herostage.cam = cam;
        this.herostage = herostage;
        this.batcher = batcher;
	}
	
	public void render() {
		
		if (herostage.hero.position.x <= 400) {
			cam.position.x = 400;
		} else if (herostage.hero.position.x >= 1200) {
			cam.position.x = 1200;
		} else {
			cam.position.x = herostage.hero.position.x;
		}
		
		cam.setViewportAndMatrices();
		renderHeroBackground(deltaTime);
		
		renderObject();
		renderControlpad();
	}
	
	public void renderHeroBackground(float deltaTime) {

		//herostage.update(deltaTime, accelX);
		//cam.position.x = FRUSTUM_WIDTH;
		//herostage.hero.position.x + 400 - herostage.hero.position.x
		batcher.beginBatch(Assets.HeroStageBackgorund);
		if (herostage.hero.position.x <= 400) {
			batcher.drawSprite(400, 240, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.heroStageBackgroundRegion);
		} else if (herostage.hero.position.x >= 1200) {
			batcher.drawSprite(1200, 240, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.heroStageBackgroundRegion);
		} else {
			batcher.drawSprite(herostage.hero.position.x, 240, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.heroStageBackgroundRegion);
		}
//		batcher.drawSprite(0, cam.position.y, 
//				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
//				Assets.heroStageBackgroundRegion);
//		batcher.drawSprite(14.9f, cam.position.y, 
//				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
//				Assets.heroStageBackgroundRegion);
//		batcher.drawSprite(29.8f, cam.position.y, 
//				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
//				Assets.heroStageBackgroundRegion);
//		batcher.drawSprite(44.7f, cam.position.y, 
//				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
//				Assets.heroStageBackgroundRegion);
//		batcher.drawSprite(59.6f, cam.position.y, 
//				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
//				Assets.heroStageBackgroundRegion);
//		batcher.drawSprite(74.5f, cam.position.y, 
//				FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
//				Assets.heroStageBackgroundRegion);
		batcher.endBatch();
	}
	
	public void renderControlpad() {
		GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.joystickbg);
		batcher.drawSprite(cam.position.x - 345, cam.position.y - 185, 90, 90, Assets.joystickbgRegion);
		batcher.endBatch();
		
		batcher.beginBatch(Assets.joystick);
		if (!herostage._dragging) {
			herostage.touchPoint.x = cam.position.x - 345;
			herostage.touchPoint.y = cam.position.y - 185;
		} else {
			if (herostage.touchPoint.x >=  cam.position.x - 345 + 45) {
				herostage.touchPoint.x = cam.position.x - 345 + 45;
			} 
			if (herostage.touchPoint.x <= cam.position.x - 345 - 45) {
				herostage.touchPoint.x = cam.position.x - 345 - 45;
			} 
			if (herostage.touchPoint.y >= cam.position.y - 185 + 45) {
				herostage.touchPoint.y = cam.position.y - 185 + 45;
			} 
			if (herostage.touchPoint.y <= cam.position.y - 185 - 45) {
				herostage.touchPoint.y = cam.position.y - 185 - 45;
			}
			if (herostage.deltaX == 0 || herostage.deltaY == 0) {
				herostage.touchPoint.x = cam.position.x - 345;
				herostage.touchPoint.y = cam.position.y - 185;
			}
		}
		
		batcher.drawSprite(herostage.touchPoint.x, herostage.touchPoint.y, 53, 53, Assets.joystickRegion);
		batcher.endBatch();
		
		batcher.beginBatch(Assets.Fire_Button);
		batcher.drawSprite(cam.position.x + 140 + 62.5f, cam.position.y - 180, 125, 100, Assets.left_firebt);
		batcher.drawSprite(cam.position.x + 140 + 62.5f + 125, cam.position.y - 180, 125, 100, Assets.right_firebt);
		batcher.endBatch();
		
		gl.glDisable(GL10.GL_BLEND);
	}
	
	public void renderObject() {
		GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.Hero);
        renderHero();
        
        batcher.endBatch();
        
        batcher.beginBatch(Assets.Thunder_Arrow);
        renderArrow();
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
	}
	
	private void renderHero() {
		TextureRegion keyFrame = null;
		switch (herostage.hero.state) {
		case Hero.Hero_state_stop:
			keyFrame = Assets.heroAnimation.getKeyFrame(herostage.hero.stateTime, 
					Animation.ANIMATION_LOOPING);
			break;
		case Hero.Hero_state_move:
			keyFrame = Assets.heroMoveAnimation.getKeyFrame(herostage.hero.stateTime,
					Animation.ANIMATION_LOOPING);
			break;
	
		case Hero.Hero_state_fire:
			keyFrame = Assets.heroFireAnimation.getKeyFrame(herostage.hero.stateTime, 
					Animation.ANIMATION_LOOPING);
			break;
		case Hero.Hero_state_moveAndfire:
			keyFrame = Assets.heroMove_FireAnimation.getKeyFrame(herostage.hero.stateTime, 
					Animation.ANIMATION_LOOPING);
			break;
		default:
		}

		float side = herostage.touchPoint.x < cam.position.x - 345 ? 1 : -1;
//		float side = 1;
//		if (herostage.touchPoint.x <= cam.position.x - 345 || arrow.state == S_Arrow.STATE_ARROW_FIRE_LEFT) {
//			side = 1;
//			batcher.drawSprite(herostage.hero.position.x, herostage.hero.position.y,
//					side * 100, 100, keyFrame);
//		} else if (herostage.touchPoint.x > cam.position.x - 345 || arrow.state == S_Arrow.STATE_ARROW_FIRE_RIGHT) {
//			side = -1;
//			batcher.drawSprite(herostage.hero.position.x, herostage.hero.position.y,
//					side * 100, 100, keyFrame);
//		}
		batcher.drawSprite(herostage.hero.position.x, herostage.hero.position.y,
				side * 100, 100, keyFrame);
				 
	}
	
	public void renderArrow() {
		TextureRegion keyFrame = null;
//		keyFrame = Assets.Thunder_Arrow_Animation.getKeyFrame(herostage.arrow.stateTime, Animation.ANIMATION_LOOPING);
//		float side2 = 1;
//		switch (herostage.arrow.state) {
//		case S_Arrow.STATE_ARROW_FIRE_LEFT:
//			keyFrame = Assets.Thunder_Arrow_Animation.getKeyFrame(herostage.arrow.stateTime, Animation.ANIMATION_LOOPING);
//			side2 = -1;
//			break;
//		case S_Arrow.STATE_ARROW_FIRE_RIGHT:
//			keyFrame = Assets.Thunder_Arrow_Animation.getKeyFrame(herostage.arrow.stateTime, Animation.ANIMATION_LOOPING);
//			side2 = 1;
//			break;
//		default:
//			break;
//		}
		
		int len = herostage.arrows.size();
		
		for(int i = 0; i < len; i++){
			
			arrow = herostage.arrows.get(i);
			keyFrame = Assets.Thunder_Arrow_Animation.getKeyFrame(arrow.stateTime, Animation.ANIMATION_LOOPING);
			batcher.drawSprite(arrow.position.x, arrow.position.y, 70, 30, keyFrame);
			
			
//			arrow.position.x += herostage.Arrow_move;
			arrow.position.x += 5;
			
		}
//		if(arrow.state == S_Arrow.STATE_ARROW_FIRE_LEFT) {
//			arrow.position.x -= 5;
//		}
//		if(arrow.state == S_Arrow.STATE_ARROW_FIRE_RIGHT) {
//			arrow.position.x += 5;
//		}
		
		
	}
	
	
}
